package com.dzh.healthflutter.flutter;

import android.os.Bundle;
import android.sax.RootElement;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.appcompat.app.AppCompatActivity;

import com.dzh.healthflutter.R;

import io.flutter.FlutterInjector;
import io.flutter.embedding.android.FlutterView;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.plugin.common.MethodChannel;

/**
 * @Description: 有重力感应的测试demo
 * @Author: duzhenhua3
 * @CreateDate: 7/27/21 6:00 PM
 */
public class FlutterViewActivity extends AppCompatActivity {

    private RelativeLayout tvFlutterViewRooot;

    private FlutterViewEngine mFlutterViewEngine;

    private FlutterView mFlutterView;

    private MethodChannel flutterChannel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flutterview);


        tvFlutterViewRooot = findViewById(R.id.tvFlutterViewRooot);

        FlutterEngine engine = new FlutterEngine(getBaseContext().getApplicationContext());
        engine.getDartExecutor().executeDartEntrypoint(new DartExecutor.DartEntrypoint(
                FlutterInjector.instance().flutterLoader().findAppBundlePath(),
                "showCell"
        ));
        mFlutterViewEngine = new FlutterViewEngine(engine);
        mFlutterViewEngine.attachToActivity(this);

        mFlutterView = new FlutterView(this);
        tvFlutterViewRooot.addView(mFlutterView);

        flutterChannel = new MethodChannel(mFlutterViewEngine.getEngine().getDartExecutor(), "dev.flutter.example/cell");

        mFlutterViewEngine.attachFlutterView(mFlutterView);

        flutterChannel.invokeMethod("setCellNumber", 100);

    }


    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    @Override
    protected void onDestroy() {
        if (mFlutterViewEngine != null) {
            mFlutterViewEngine.detachFlutterView();
            mFlutterViewEngine.detachActivity();
        }
        super.onDestroy();
    }
}
